package com.eamois.ui.manual;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;

import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import com.eamois.action.manual.CatalogAction;
import com.eamois.domain.manual.Catalog;
import com.eamois.domain.manual.CatalogStyle;
import com.eamois.domain.manual.CatalogStyleField;
import com.eamois.domain.manual.Technology;
import com.mit.control.MitMenuItem;
import com.mit.control.MitOptionPane;
import com.mit.control.MitPanel;
import com.mit.control.MitPopupMenu;
import com.mit.control.MitScrollPane;
import com.mit.control.MitSplitPane;
import com.mit.control.MitTree;
import com.mit.plm.client.publicClass.MitPDMDialog;
import com.mit.plm.client.publicClass.MitPDMFrame;

public class FrmCatalog extends MitPDMDialog {

	private MitPanel contentPane;
	private CatalogAction catalogAction;
	private Collection<Catalog> catalogs;
	private MitPanel pnlContainer;
	private MitTree trCatalog;
	private MitPopupMenu popupMenu;
	private MitMenuItem itmAddCatalog;
	private MitMenuItem itmUpdateCatalog;
	private MitMenuItem itmDeleteCatalog;
	private MitMenuItem itmAddCatalogStyle;
	private MitMenuItem itmUpdateCatalogStyle;
	private MitMenuItem itmDeleteCatalogStyle;
	private MitMenuItem itmAddCatalogStyleField;
	private MitMenuItem itmUpdateCatalogStyleField;
	private MitMenuItem itmDeleteCatalogStyleField;
	private Technology tech;
	private boolean isCommit ;

	public boolean isCommit() {
		return isCommit;
	}

	public void setCommit(boolean isCommit) {
		this.isCommit = isCommit;
	}

	public Technology getTech() {
		return tech;
	}

	public void setTech(Technology tech) {
		this.tech = tech;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			// TODO: handle exception
		}
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					FrmCatalog frame = new FrmCatalog(new Technology());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public FrmCatalog getCurrentFrmCatalog() {
		return this;
	}

	public void insertCatalogNode(Catalog catalog) {
		DefaultMutableTreeNode node = new DefaultMutableTreeNode(catalog);
		DefaultTreeModel model = (DefaultTreeModel) this.trCatalog.getModel();
		DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
		root.insert(node, root.getChildCount());
		this.trCatalog.updateUI();
	}

	public void insertCatalogStyle(CatalogStyle catalogStyle) {
		DefaultMutableTreeNode node = new DefaultMutableTreeNode(catalogStyle);
		DefaultMutableTreeNode parent = (DefaultMutableTreeNode) this
				.getTrCatalog().getSelectionPath().getLastPathComponent();
		parent.insert(node, parent.getChildCount());
		this.getTrCatalog().updateUI();
	}

	public void insertCatalogStyleField(CatalogStyleField catalogStyleField) {
		DefaultMutableTreeNode node = new DefaultMutableTreeNode(
				catalogStyleField);
		DefaultMutableTreeNode parent = (DefaultMutableTreeNode) this
				.getTrCatalog().getSelectionPath().getLastPathComponent();
		parent.insert(node, parent.getChildCount());
		this.getTrCatalog().updateUI();
	}

	public void destroyContainer() {
		this.pnlContainer.removeAll();
		this.pnlContainer.updateUI();

		this.trCatalog.updateUI();
	}

	/**
	 * Create the frame.
	 */
	public FrmCatalog(Technology tech) {
		setTech(tech);
		initialize();
	}

	private void initialize() {
		setTitle("目录设置");
		setBounds(100, 100, 686, 509);
		contentPane = new MitPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		MitPanel panel = new MitPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));

		MitPanel panel_1 = new MitPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));

		MitSplitPane splitPane = new MitSplitPane();
		panel_1.add(splitPane, BorderLayout.CENTER);

		MitPanel panel_2 = new MitPanel();
		splitPane.setLeftComponent(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));

		trCatalog = new MitTree();
		trCatalog.addTreeSelectionListener(new TreeSelectionListener() {
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				TreePath path = getTrCatalog().getSelectionPath();
				if (path == null) {
					return;
				}
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
						.getLastPathComponent();
				if (node.getUserObject() instanceof Catalog) {

				} else if (node.getUserObject() instanceof CatalogStyle) {

				} else if (node.getUserObject() instanceof CatalogStyleField) {

				}
			}
		});
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("目录");
//		this.setCatalogs(this.getCatalogAction().findAllCatalog());
		this.setCatalogs(this.getCatalogAction().findAllCatalogByTech(getTech()));
		for (Catalog catalog : getCatalogs()) {
			DefaultMutableTreeNode catalogNode = new DefaultMutableTreeNode(
					catalog);
			root.add(catalogNode);

			Collection<CatalogStyle> catalogStyles = this.getCatalogAction()
					.findCatalogStyle(catalog);
			for (CatalogStyle catalogStyle : catalogStyles) {
				DefaultMutableTreeNode styleNode = new DefaultMutableTreeNode(
						catalogStyle);
				catalogNode.add(styleNode);

				Collection<CatalogStyleField> fields = this.getCatalogAction()
						.findCatalogStyleField(catalogStyle);
				for (CatalogStyleField field : fields) {
					DefaultMutableTreeNode fieldNode = new DefaultMutableTreeNode(
							field);
					styleNode.add(fieldNode);
				}
			}
		}
		DefaultTreeModel treeModel = new DefaultTreeModel(root);
		trCatalog.setModel(treeModel);

		MitScrollPane scrollPane = new MitScrollPane();
		panel_2.add(scrollPane, BorderLayout.CENTER);
		scrollPane.setViewportView(trCatalog);

		popupMenu = new MitPopupMenu();
		addPopup(trCatalog, popupMenu);

		itmAddCatalog = new MitMenuItem("添加目录");
		itmAddCatalog.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				destroyContainer();
				PnlEditCatalog pnl = new PnlEditCatalog(getCurrentFrmCatalog(),getTech());
				pnlContainer.add(pnl, BorderLayout.NORTH);
				pnlContainer.updateUI();
				if(pnl.isCommit()){
					setCommit(true);
					
				}
			}
		});
		popupMenu.add(itmAddCatalog);

		itmUpdateCatalog = new MitMenuItem("修改目录");
		itmUpdateCatalog.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				destroyContainer();
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) trCatalog
						.getSelectionPath().getLastPathComponent();
				PnlEditCatalog pnl = new PnlEditCatalog(getCurrentFrmCatalog(),
						(Catalog) node.getUserObject());
				pnlContainer.add(pnl, BorderLayout.NORTH);
				pnlContainer.updateUI();
				if(pnl.isCommit()){
					setCommit(true);
					
				}
			}
		});
		popupMenu.add(itmUpdateCatalog);

		itmDeleteCatalog = new MitMenuItem("删除目录");
		itmDeleteCatalog.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) trCatalog
						.getSelectionPath().getLastPathComponent();
				Catalog catalog = (Catalog) node.getUserObject();
				Collection<CatalogStyle> styles = getCatalogAction()
						.findCatalogStyle(catalog);
				if (styles.size() != 0) {
					MitOptionPane.showMessageDialog(null, "请先删除目录样式再删除目录");
					return;
				}
				if (MitOptionPane.showConfirmDialog(null, "确定删除选中的目录", "确定",
						JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
					return;
				}
				try {

					getCatalogAction().removeCatalog(catalog);
				} catch (Exception e2) {
					// TODO: handle exception
					MitOptionPane.showMessageDialog(null, "该目录存在引用，不能直接删除!");
				}
				DefaultTreeModel model = (DefaultTreeModel) getTrCatalog()
						.getModel();
				model.removeNodeFromParent(node);
				getTrCatalog().updateUI();
				setCommit(true);
			}
		});
		popupMenu.add(itmDeleteCatalog);

		JSeparator separator = new JSeparator();
		// popupMenu.add(separator);

		itmAddCatalogStyle = new MitMenuItem("添加样式");
		itmAddCatalogStyle.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				destroyContainer();
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) getTrCatalog()
						.getSelectionPath().getLastPathComponent();
				PnlEditCatalogStyle pnlCatalogStyle = new PnlEditCatalogStyle(
						getCurrentFrmCatalog(), (Catalog) node.getUserObject());
				getPnlContainer().add(pnlCatalogStyle, BorderLayout.NORTH);
				getPnlContainer().updateUI();
			}
		});
		// popupMenu.add(itmAddCatalogStyle);

		itmUpdateCatalogStyle = new MitMenuItem("修改样式");
		itmUpdateCatalogStyle.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				destroyContainer();
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) getTrCatalog()
						.getSelectionPath().getLastPathComponent();
				DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node
						.getParent();
				PnlEditCatalogStyle pnlCatalogStyle = new PnlEditCatalogStyle(
						getCurrentFrmCatalog(), (Catalog) parentNode
								.getUserObject(), (CatalogStyle) node
								.getUserObject());
				getPnlContainer().add(pnlCatalogStyle, BorderLayout.NORTH);
				getPnlContainer().updateUI();
			}
		});
		// popupMenu.add(itmUpdateCatalogStyle);

		itmDeleteCatalogStyle = new MitMenuItem("删除样式");
		itmDeleteCatalogStyle.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) getTrCatalog()
						.getSelectionPath().getLastPathComponent();
				CatalogStyle style = (CatalogStyle) node.getUserObject();
				Collection<CatalogStyleField> fields = getCatalogAction()
						.findCatalogStyleField(style);
				if (fields.size() != 0) {
					MitOptionPane.showMessageDialog(null, "请先删除样式字段再删除样式");
					return;
				}
				if (MitOptionPane.showConfirmDialog(null, "确定删除选择的目录样式", "确定",
						JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
					return;
				}
				try {

					getCatalogAction().removeCatalogStyle(style);
				} catch (Exception e2) {
					// TODO: handle exception
					MitOptionPane.showMessageDialog(null, "该样式存在引用，不能直接删除！");
				}
				DefaultTreeModel model = (DefaultTreeModel) getTrCatalog()
						.getModel();
				model.removeNodeFromParent(node);
				getTrCatalog().updateUI();
			}
		});
		// popupMenu.add(itmDeleteCatalogStyle);

		JSeparator separator_1 = new JSeparator();
		// popupMenu.add(separator_1);

		itmAddCatalogStyleField = new MitMenuItem("添加字段");
		itmAddCatalogStyleField.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				destroyContainer();
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) getTrCatalog()
						.getSelectionPath().getLastPathComponent();
				PnlEditCatalogStyleField pnl = new PnlEditCatalogStyleField(
						getCurrentFrmCatalog(), (CatalogStyle) node
								.getUserObject());
				getPnlContainer().add(pnl, BorderLayout.NORTH);
				getPnlContainer().updateUI();
			}
		});
		// popupMenu.add(itmAddCatalogStyleField);

		itmUpdateCatalogStyleField = new MitMenuItem("修改字段");
		itmUpdateCatalogStyleField.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				destroyContainer();
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) getTrCatalog()
						.getSelectionPath().getLastPathComponent();
				PnlEditCatalogStyleField pnl = new PnlEditCatalogStyleField(
						getCurrentFrmCatalog(), (CatalogStyleField) node
								.getUserObject());
				getPnlContainer().add(pnl, BorderLayout.NORTH);
				getPnlContainer().updateUI();
			}
		});
		// popupMenu.add(itmUpdateCatalogStyleField);

		itmDeleteCatalogStyleField = new MitMenuItem("删除字段");
		itmDeleteCatalogStyleField.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) getTrCatalog()
						.getSelectionPath().getLastPathComponent();
				CatalogStyleField field = (CatalogStyleField) node
						.getUserObject();

				if (MitOptionPane.showConfirmDialog(null, "确定删除选中的字段", "确定",
						JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
					return;
				}
				try {
					getCatalogAction().removeCatalogStyleField(field);
				} catch (Exception e2) {
					// TODO: handle exception
					MitOptionPane.showMessageDialog(null, "该字段存在引用，不能直接删除！");
				}
				DefaultTreeModel model = (DefaultTreeModel) getTrCatalog()
						.getModel();
				model.removeNodeFromParent(node);
				getTrCatalog().updateUI();
			}
		});
		// popupMenu.add(itmDeleteCatalogStyleField);

		MitPanel panel_3 = new MitPanel();
		splitPane.setRightComponent(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));

		pnlContainer = new MitPanel();
		panel_3.add(pnlContainer, BorderLayout.NORTH);
		pnlContainer.setLayout(new BorderLayout(0, 0));
		splitPane.setDividerLocation(200);
	}

	private void addPopup(final Component component, final MitPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				MitTree tree = (MitTree) component;
				TreePath path = tree.getSelectionPath();
				if (path == null) {
					return;
				}
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
						.getLastPathComponent();
				if (node.getUserObject() instanceof String) {
					getItmAddCatalog().setEnabled(true);
					getItmDeleteCatalog().setEnabled(false);
					getItmUpdateCatalog().setEnabled(false);
					getItmAddCatalogStyle().setEnabled(false);
					getItmDeleteCatalogStyle().setEnabled(false);
					getItmUpdateCatalogStyle().setEnabled(false);
					getItmAddCatalogStyleField().setEnabled(false);
					getItmDeleteCatalogStyleField().setEnabled(false);
					getItmUpdateCatalogStyleField().setEnabled(false);
				} else if (node.getUserObject() instanceof Catalog) {
					getItmAddCatalog().setEnabled(false);
					getItmDeleteCatalog().setEnabled(true);
					getItmUpdateCatalog().setEnabled(true);
					getItmAddCatalogStyle().setEnabled(true);
					getItmDeleteCatalogStyle().setEnabled(false);
					getItmUpdateCatalogStyle().setEnabled(false);
					getItmAddCatalogStyleField().setEnabled(false);
					getItmDeleteCatalogStyleField().setEnabled(false);
					getItmUpdateCatalogStyleField().setEnabled(false);

				} else if (node.getUserObject() instanceof CatalogStyle) {
					getItmAddCatalog().setEnabled(false);
					getItmDeleteCatalog().setEnabled(false);
					getItmUpdateCatalog().setEnabled(false);
					getItmAddCatalogStyle().setEnabled(false);
					getItmDeleteCatalogStyle().setEnabled(true);
					getItmUpdateCatalogStyle().setEnabled(true);
					getItmAddCatalogStyleField().setEnabled(true);
					getItmDeleteCatalogStyleField().setEnabled(false);
					getItmUpdateCatalogStyleField().setEnabled(false);

				} else if (node.getUserObject() instanceof CatalogStyleField) {
					getItmAddCatalog().setEnabled(false);
					getItmDeleteCatalog().setEnabled(false);
					getItmUpdateCatalog().setEnabled(false);
					getItmAddCatalogStyle().setEnabled(false);
					getItmDeleteCatalogStyle().setEnabled(false);
					getItmUpdateCatalogStyle().setEnabled(false);
					getItmAddCatalogStyleField().setEnabled(false);
					getItmDeleteCatalogStyleField().setEnabled(true);
					getItmUpdateCatalogStyleField().setEnabled(true);
				}
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}

	public CatalogAction getCatalogAction() {
		if (catalogAction == null) {
			catalogAction = new CatalogAction();
		}
		return catalogAction;
	}

	public void setCatalogAction(CatalogAction catalogAction) {
		this.catalogAction = catalogAction;
	}

	public Collection<Catalog> getCatalogs() {
		return catalogs;
	}

	public void setCatalogs(Collection<Catalog> catalogs) {
		this.catalogs = catalogs;
	}

	public MitPopupMenu getPopupMenu() {
		return popupMenu;
	}

	public MitMenuItem getItmAddCatalog() {
		return itmAddCatalog;
	}

	public MitMenuItem getItmUpdateCatalog() {
		return itmUpdateCatalog;
	}

	public MitMenuItem getItmDeleteCatalog() {
		return itmDeleteCatalog;
	}

	public MitMenuItem getItmAddCatalogStyle() {
		return itmAddCatalogStyle;
	}

	public MitMenuItem getItmUpdateCatalogStyle() {
		return itmUpdateCatalogStyle;
	}

	public MitMenuItem getItmDeleteCatalogStyle() {
		return itmDeleteCatalogStyle;
	}

	public MitMenuItem getItmAddCatalogStyleField() {
		return itmAddCatalogStyleField;
	}

	public MitMenuItem getItmUpdateCatalogStyleField() {
		return itmUpdateCatalogStyleField;
	}

	public MitMenuItem getItmDeleteCatalogStyleField() {
		return itmDeleteCatalogStyleField;
	}

	public MitTree getTrCatalog() {
		return trCatalog;
	}

	public MitPanel getPnlContainer() {
		return pnlContainer;
	}
}
